<style>
.clear {
	clear: both;
}
.hide {
	display: none;
}
.width50 {
	width: 50px!important;
}
.width70p {
	width: 75%;
}
.width100p {
	width: 100%;
}
.width150 {
	width:150px;
}
.left {
	float:left;
}
.margin-R-15 {
	margin-right: 15px;
}
.nopad {
	padding: 0;
	text-align: center;
}
.label_simple {
	float:none!important;
	display:inline!important;
	font-weight: normal!important;
}
#publication_information_0 {
	border-top: 0px solid #cccccc;
	margin-top: 0px;
	padding-top: 0px;
}
.publication_information_component {
	border-top: 1px solid #cccccc;
	margin-top: 5px;
	padding-top: 5px;
}
</style>
<div class="componentheading"><?php echo $page_title;?></div>
<div>
     <?php global_message(); ?>
</div>
<?php


	$isEdit = isset($_borrower) ? true : false;

	$text = empty($isEdit) ? 'Add' : 'Update';

	$selected = 'selected = "selected"';

?>
<form method="post" id="newForm">
     <div class="box table">
          <table width="100%">
               <tr class="box-header">
                    <td colspan="2"><strong>General User Information</strong></td>
               </tr>
               <tr>
                    <td><strong>User Type</strong></td>
                    <td><select name="user_type" onchange="changeUserType(this.value);" id="borrower_user_type">
                              <option value="non-member" <?php if($isEdit){ if($_borrower->member_id ==0) echo $selected;}?>>Non-Member</option>
                              <option value="member" <?php if($isEdit){ if($_borrower->member_id !=0) echo $selected;}?>>Member</option>
                         </select></td>
               </tr>
               <tr class="non_member_field">
                    <td><strong>First Name</strong></td>
                    <td><input type="text" class="required hidden" name="first_name" id="first_name" value="<?php if($isEdit) echo $_borrower->first_name;?>" /></td>
               </tr>
               <tr class="non_member_field">
                    <td><strong>Last Name</strong></td>
                    <td><input type="text" class="required hidden" name="last_name" id="last_name" value="<?php if($isEdit) echo $_borrower->last_name;?>" /></td>
               </tr>
               <tr class="non_member_field">
                    <td><strong>Email</strong></td>
                    <td><input type="text" class="required email hidden" name="email" id="email" value="<?php if($isEdit) echo $_borrower->email;?>" /></td>
               </tr>
               <tr class="non_member_field">
                    <td><strong>Address1</strong></td>
                    <td><input type="text" class="required hidden" name="address1" id="address1" value="<?php if($isEdit) echo $_borrower->address1;?>" /></td>
               </tr>
               <tr class="non_member_field">
                    <td><strong>Address2</strong></td>
                    <td><input type="text" class="hidden" name="address2" id="address2" value="<?php if($isEdit) echo $_borrower->address2;?>" /></td>
               </tr>
               <tr class="non_member_field">
                    <td><strong>Phone1</strong></td>
                    <td><input type="text" class="required hidden" name="phone1" id="phone1" value="<?php if($isEdit) echo $_borrower->phone1;?>" /></td>
               </tr>
               <tr class="non_member_field">
                    <td><strong>Phone2</strong></td>
                    <td><input type="text" class="hidden" name="phone2" id="phone2" value="<?php if($isEdit) echo $_borrower->phone2;?>" /></td>
               </tr>
               <tr class="member_field hide">
                    <td>Member ID</td>
                    <td><select name="member_id">
                              <?php foreach($member_list as $member_id=>$member_name){?>
                              <option value="<?php echo $member_id;?>" <?php if($isEdit){ if($_borrower->member_id == $member_id) echo $selected;}?>><?php echo $member_name;?></option>
                              <?php }?>
                         </select></td>
               </tr>
               <tr class="box-header">
                    <td colspan="2"><strong>Publication Information</strong></td>
               </tr>
               <?php if($isEdit) : ?>
               <?php
		$publication_details = $this->borrower_model->get_publication_list($_borrower->id);

		//debug_array($publication_details);

		foreach($publication_details as $key=>$publication) :
	?>
               <tr>
                    <td colspan="2"><input type="hidden" name="borrow[<?php echo $key;?>][borrow_id]" value="<?php echo $publication->id;?>" />
                         <div class="left width150 margin-R-15">
                              <label><?php echo $publication->title;?></label>
                              <div style="clear:left;">Type : <?php echo $publication->type;?></div>
                         </div>
                         <div class="left width70p">
                              <div class="width100p" style="padding-bottom:3px;">
                                   <div class="left width150">
                                        <label>Refundable</label>
                                   </div>
                                   <div class="left width150">
                                        <input type="text" name="borrow[<?php echo $key;?>][refundable_qty]" value="<?php echo $publication->refundable_qty;?>" style="width:100px;" readonly="readonly" class="readonly" />
                                   </div>
                                   <div> Return :
                                        <input type="text" name="borrow[<?php echo $key;?>][return_qty]" value="<?php echo $publication->return_qty;?>" style="width:100px;" />
                                   </div>
                                   <div class="clear"></div>
                              </div>
                              <div class="width100p">
                                   <div class="left width150">
                                        <label>Non-Refundable</label>
                                   </div>
                                   <div class="left width150">
                                        <input type="text" name="borrow[<?php echo $key;?>][non_refundable_qty]" value="<?php echo $publication->non_refundable_qty;?>" style="width:100px;" readonly="readonly" class="readonly" />
                                   </div>
                                   <div class="clear"></div>
                              </div>
                         </div></td>
               </tr>
               <?php endforeach; ?>
               <?php endif; ?>
               <tr>
                    <td colspan="2" id="publication_information"></td>
               </tr>
               <tr>
                    <td>&nbsp;</td>
                    <td><input type="button" onclick="addNewBorrower(cntNo);" value="Add more" /></td>
               </tr>
               <tr>
                    <td>&nbsp;</td>
                    <td><input type="submit" name="submit_borrower" id="submit_borrower" value="<?php echo $text;?>">
                         <?php if($isEdit): ?>
                         <input type="button" onclick="window.location.href = '<?php echo filter_url('edit');?>'" value="Back" />
                         <?php else: ?>
                         <input type="button" onclick="window.location.href = '<?php echo site_url('borrower');?>'" value="Back" />
                         <?php endif; ?></td>
               </tr>
          </table>
     </div>
</form>
<script>

var cntNo=0;

$(document).ready(function(){

		$('#newForm').validate();

        defaultVal = $("#borrower_user_type").val();

        changeUserType(defaultVal);

		<?php if(!$isEdit) : ?>
	        addNewBorrower(cntNo);
		<?php endif; ?>

});



function addNewBorrower(No){

    var loading_image = "<img src='<?php echo base_url().'/assets/images/fb_loading.gif'?>'>";

    var divID = "publication_information_"+No;

    $("#publication_information").append("<div id='"+divID+"' class='publication_information_component'></div>");

    $("#"+divID).html(loading_image);

    $.ajax({

               type: "POST",

               url: "<?php echo site_url('borrower/ajax_add_borrower_form');?>",

               data: 'cntNo='+No,

               success: function(msg){

                   $("#"+divID).html(msg);

                   changePublicationMax($("#borrower_publication_list_"+No).val(),+No);

               }

           });

      cntNo++;

}

function changeUserType(user_type)
{
    if(user_type=="member"){

        $(".non_member_field").addClass('hide');

        $(".member_field").removeClass('hide');

		$('.hidden').attr('disabled', 'disabled');

		$('.hidden').val('');

    }else{

        $(".non_member_field").removeClass('hide');

        $(".member_field").addClass('hide');

		$('.hidden').removeAttr('disabled');
    }

}

function changePublicationMax(publication_id,ID){
	$('#borrower_publication_list_'+ID).attr('disabled', 'disabled');
    $.ajax({

               type: "POST",

               url: "<?php echo site_url('borrower/ajax_get_max_publication');?>",

               data: 'publication_id='+publication_id,

               success: function(msg){

                   var msg1=msg.split('~~');

                   //var qty_box_en=msg1[1];

                  // var qty_box_np=msg1[2];

                   $(".max_en_qty_box_"+ID).html('(max:'+msg1[1]+')');

                   $(".max_np_qty_box_"+ID).html('(max:'+msg1[2]+')');

				   $(".max_en_qty_distribution_"+ID).html('(max:'+msg1[3]+')');

				   $(".max_np_qty_distribution_"+ID).html('(max:'+msg1[4]+')');

				   $('#borrower_publication_list_'+ID).removeAttr('disabled');

               }

           });

}

</script>
